import request from '../utils/request';
import { PAGE_SIZE, API_PREFIX } from '../constants';
import qs from 'qs';

export function fetch({ page }) {
  return request(`${API_PREFIX}/users?_page=${page}&_limit=${PAGE_SIZE}`);
}

export function create(values) {
  return request(`${API_PREFIX}/users`, {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    method: 'POST',
    body: JSON.stringify(values),
  });
}

export function query({ values, page }) {
  return request(`${API_PREFIX}/users/query?_page=${page}&_limit=${PAGE_SIZE}`, {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    method: 'POST',
    body: JSON.stringify(values),
  });
}

export function remove(id) {
  return request(`${API_PREFIX}/users/remove`, {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: 'POST',
    body: `id=${id}`,
  });
}

export function patch({ id, values }) {
  return request(`${API_PREFIX}/users/patch`, {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: 'POST',
    body: qs.stringify(Object.assign(values, { id: id })),
  });
}

export function getDict({ type }) {
  return request(`${API_PREFIX}/getDict?type=${type}`);
}
